<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of systemclass
 *
 * @author Administrator
 */
 
if(!defined('IN_SITE')) {
	exit('Access Denied');
}
class cores {
    //put your code here
    var $db;
    var $m_error_level=1;
    function __construct() {
        //$this->db = $GLOBALS['db'];
    }
    function db() {
        if(!$this->db) {
            $this->db = $GLOBALS['db'];
        }
        return $this->db;
    }

    function formart_date($date,$timearea) {

    }
    /**
     * 报告错误信息
     * @global float $time_start
     * @param string $err
     */
    function report_error($title,$msg) {
        sendmail($GLOBALS['adminemail'], $title, $msg);
    }

    /**
     * 存储错误信息
     * @global float $time_start
     * @param string $err
     */
    function make_error($err,$level=1) {
        global $time_start;

        if($this->m_error_level >= $level) {

            if($this->m_showdtime) {
                $use_time = '('.get_use_time(true).'s)';
            }

            if($this->mShowType=='echo') {
                echo '<p>'.$err .$use_time.'</p>'."\r\n";
                echo "\r\n";
                flush();
            }else {
                $this->m_error[] = $err. $use_time;
            }
        }
    }
    /**
     * 得到错误消息
     * @param string $format
     * @return
     */
    function get_error($format='array') {
        if(!$this->m_error)return;
        if($format=='array') {
            return $this->m_error;
        }else {

            $errors ='';
            foreach($this->m_error as $key=>$value) {
                $errors .= '<p>'.($key+1) .'. '.$value.'</p>'."\r\n";
            }
            return $errors;
        }
    }

    /**
     * 对象转换成数组
     * @param object $obj
     * @return array
     */
    function obj2array($obj=NULL) {
        if($obj) {
            $_array = (array)$obj;
        }else {
            $_array = (array)$this;
        }
        foreach($_array as $k=>$v) {
            $_array2[str_replace('m_','',$k)] = $v;
        }
        return $_array2;
    }

    /**
     * 取系统配置
     * @param <type> $expired_time
     * @return <type> 
     */
    function get_system_config($expired_time=0) {
        $file ='system_config.php';
        if($expired_time) {
            //$status = cache_isset($file,$expired_time);
            $status = true;
        }else {
            $status =false;
        }

        if($status==false) {
            $sql="SELECT * FROM ".TABLE_CONFIG." ";
            //$arr = $this->db()->fetch_all($sql);
            $arr = $GLOBALS['db']->fetch_all($sql);
            foreach($arr  as $v) {
                $arrs[$v['name']] = $v['value'];
            }
            cache_write($file,$arrs);
        }else {
            $arrs = cache_read($file);
        }
        return $arrs;
    }
    /**
     * 设置系统变量
     * @param string $name 变量名
     * @param string $value 值
     * @return int 影响行数
     */
    function set_system_config($name,$value) {
        $sql="SELECT id FROM ".TABLE_CONFIG." WHERE name='".mysql_escape_string($name)."' ";
        if(!$GLOBALS['db']->result_first($sql)) {
            $dbarr['value']= $value;
            $dbarr['name'] = $name;
            $sql = make_insert_sql(TABLE_CONFIG,$dbarr,$where);
            $GLOBALS['db']->query($sql);
        }
        $dbarr['value']=$value;
        $where['name'] = $name;
        $sql = make_update_sql(TABLE_CONFIG,$dbarr,$where);
        //echo $sql;
        $GLOBALS['db']->query($sql);
        return $GLOBALS['db']->affected_rows();
    }
    /**
     * 设置语言
     * @param string $lang 指定语言
     */
    function language_setting($lang='') {
        $lang_arr=array(1=>'en',2=>'zh-cn',3=>'zh-tw',4=>'id-id',5=>'vi-vn',6=>'th-th',7=>'ja-jp',8=>'ko-kr',9=>'de-de',10=>'es-es',11=>'my-mm');
        $agent2_lang_arr=array('EN'=>'en','ZH_CN'=>'zh-cn','ZH_TW'=>'zh-tw','VI_VN'=>'vi-vn','TH_TH'=>'th-th');
        $agent_lang_arr=array('0'=>'EN','1'=>'ZH_CN','2'=>'ZH_TW','3'=>'VI_VN','4'=>'TH_TH');
        //$lang = '';
        /*
	//不检测，由rewrite定义
	$lang = cut($_SERVER['SCRIPT_URL'],'/','/');//前台
	if(!in_array($lang,  $lang_arr)	) {
		$lang= '';
	}*/

        // !$lang &&
        if(!$lang) {
            if(isset($_GET['lang'])) {//代理前台和会员后台切换（前台也可以使用）
                $lang = $_GET['lang'];
            }elseif(isset($_GET['hLangID'])) {//代理页面//http://45acjjn8gaxf.sbobet.com/webroot/restricted/hometop.aspx?select=EN&hLangID=EN&hToCustID=
                $agent_lang = $_GET['hLangID'];
                $lang = $agent2_lang_arr[$agent_lang];
            }
        }

        if(!empty($lang)) {
            $lang = strtolower($lang);

        }elseif($_SESSION['display_lang']) {
            $lang =$_SESSION['display_lang'];
        }

        if(empty($lang)) {//如果没有SESSION又没有指定，则自动使用浏览器语言
            $lang = self::get_accept_language();
        }

        if(!in_array($lang, $lang_arr)) {//如果不是系统支持的语言，则显示EN
            $lang = 'en';
        }
        $lang_id =array_search($lang, $lang_arr);
        $_SESSION['display_lang_id'] = $lang_id;
        $_SESSION['display_lang']     = $lang;
        // include(SITE_ROOT.'/includes/language/'.$lang.'.inc.php');
    }
    /**
     * 自动取浏览器支持的语言
     * @return string
     */
    function get_accept_language() {
        // 分析 HTTP_ACCEPT_LANGUAGE 的属性
        // 这里只取第一语言设置 （其他可根据需要增强功能，这里只做简单的方法演示）

        preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches);
        $lang = $matches[1];
        return $lang;

    }
    /**
     * 检查路径（不能为远程路径）
     * @param <type> $path
     * @return <type>
     */
    function check_path($path) {
        if(stristr($path,'http:') || stristr($path,'ftp:')  ) {
            exit("The system cannot find the file specified.");
        }else {
            return $path;
        }
    }

    /**
     * 将数组以JSON方式输出
     * @param array $arr
     */
    function out_json($arrs) {
        $script = json_encode($arrs);
        header('Content-Type:application/json');
        echo $script;
    }
}
?>